<?php

namespace Yo\ToolBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;

use Yo\ToolBundle\Entity\Like;
use Yo\ToolBundle\Form\LikeType;

/**
 * Like controller.
 *
 * @Route("/like")
 */
class LikeController extends Controller
{
    /**
     * Lists all Like entities.
     *
     * @Route("/", name="like")
     * @Template()
     */
    public function indexAction()
    {
        $em = $this->getDoctrine()->getEntityManager();

        $entities = $em->getRepository('YoToolBundle:Like')->findAll();

        return array('entities' => $entities);
    }

    /**
     * Finds and displays a Like entity.
     *
     * @Route("/{id}/show", name="like_show")
     * @Template()
     */
    public function showAction($id)
    {
        $em = $this->getDoctrine()->getEntityManager();

        $entity = $em->getRepository('YoToolBundle:Like')->find($id);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find Like entity.');
        }

        $deleteForm = $this->createDeleteForm($id);

        return array(
            'entity'      => $entity,
            'delete_form' => $deleteForm->createView(),        );
    }

    /**
     * Displays a form to create a new Like entity.
     *
     * @Route("/new", name="like_new")
     * @Template()
     */
    public function newAction() {
        $like = new Like();
        $form = $this->createForm(new LikeType(), $like);
		
		if (!$this->get('request')->isXmlHttpRequest()) {
			return array(
				'like' => $like,
				'form' => $form->createView()
			);
			
		} else {
			die('KO');
		}
    }

    /**
     * Creates a new Like entity.
     *
     * @Route("/create", name="like_create")
     * @Method("post")
     * @Template("YoToolBundle:Like:new.html.twig")
     */
    public function createAction()
    {
        $entity  = new Like();
        $request = $this->getRequest();
        $form    = $this->createForm(new LikeType(), $entity);
        $form->bindRequest($request);

        if ($form->isValid()) {
            $em = $this->getDoctrine()->getEntityManager();
            $em->persist($entity);
            $em->flush();

            return $this->redirect($this->generateUrl('like_show', array('id' => $entity->getId())));
            
        }

        return array(
            'entity' => $entity,
            'form'   => $form->createView()
        );
    }

    /**
     * Displays a form to edit an existing Like entity.
     *
     * @Route("/{id}/edit", name="like_edit")
     * @Template()
     */
    public function editAction($id)
    {
        $em = $this->getDoctrine()->getEntityManager();

        $entity = $em->getRepository('YoToolBundle:Like')->find($id);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find Like entity.');
        }

        $editForm = $this->createForm(new LikeType(), $entity);
        $deleteForm = $this->createDeleteForm($id);

        return array(
            'entity'      => $entity,
            'edit_form'   => $editForm->createView(),
            'delete_form' => $deleteForm->createView(),
        );
    }

    /**
     * Edits an existing Like entity.
     *
     * @Route("/{id}/update", name="like_update")
     * @Method("post")
     * @Template("YoToolBundle:Like:edit.html.twig")
     */
    public function updateAction($id)
    {
        $em = $this->getDoctrine()->getEntityManager();

        $entity = $em->getRepository('YoToolBundle:Like')->find($id);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find Like entity.');
        }

        $editForm   = $this->createForm(new LikeType(), $entity);
        $deleteForm = $this->createDeleteForm($id);

        $request = $this->getRequest();

        $editForm->bindRequest($request);

        if ($editForm->isValid()) {
            $em->persist($entity);
            $em->flush();

            return $this->redirect($this->generateUrl('like_edit', array('id' => $id)));
        }

        return array(
            'entity'      => $entity,
            'edit_form'   => $editForm->createView(),
            'delete_form' => $deleteForm->createView(),
        );
    }

    /**
     * Deletes a Like entity.
     *
     * @Route("/{id}/delete", name="like_delete")
     * @Method("post")
     */
    public function deleteAction($id)
    {
        $form = $this->createDeleteForm($id);
        $request = $this->getRequest();

        $form->bindRequest($request);

        if ($form->isValid()) {
            $em = $this->getDoctrine()->getEntityManager();
            $entity = $em->getRepository('YoToolBundle:Like')->find($id);

            if (!$entity) {
                throw $this->createNotFoundException('Unable to find Like entity.');
            }

            $em->remove($entity);
            $em->flush();
        }

        return $this->redirect($this->generateUrl('like'));
    }

    private function createDeleteForm($id)
    {
        return $this->createFormBuilder(array('id' => $id))
            ->add('id', 'hidden')
            ->getForm()
        ;
    }
}
